/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oimwrapper.testers;

import Thor.API.Operations.tcRequestOperationsIntf;
import Thor.API.tcResultSet;
import com.thortech.util.logging.Logger;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.oimwrapper.connectors.OIMAdapterBase;
import org.oimwrapper.connectors.OIMRemoteClientBase;

/**
 *
 */
public class ClientRequestKillerTester extends ClientBaseTester {
    
    private static final Logger logger = Logger.getLogger(ClientCustomRequestTester.class.getName());

    private OIMAdapterBase apiBase;
    private OIMRemoteClientBase tester;
    private tcRequestOperationsIntf requestOps;
    
    @Test
    public void mainTest()
    {
        
        
        try {
            tester = this.connect();
            apiBase = tester.getUtils();
        } catch (Exception ex) {
            logger.error("Exception",ex);
            return;
        }
        
        logger.debug("Connected!");
        
        try
        {
            requestOps = (tcRequestOperationsIntf)tester.getUtils().getUtility(Thor.API.Operations.tcRequestOperationsIntf.class);
        }
        catch(Exception e)
        {
            logger.error("Exception",e);
        }
        
        //cancelRequest(4011L);
        //findRequestsForTargetUser(541L);
        getPendingApprovals(1L);
        
    }
    
    public void getPendingApprovals(long userId)
    {
        try
        {
            //String[] orderBy = new String[]{"Pending.Approval.Request.Key"};
            String[] orderBy = new String[]{"OTI.REQ_KEY"};
            Map crit = new HashMap();
            //crit.put("MIL_NAME","Cancel Task");
            //crit.put("Requests.Target Type","U");
            logger.debug("Paged QUERY");
            tcResultSet rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(userId, crit, 1, 100, orderBy, true, true);
            printResultSet(rs);
            logger.debug("NONPaged QUERY");
            rs = requestOps.getPendingApprovalTasksAssignedToUser(userId, crit);
            printResultSet(rs);
        }
        catch(Exception e)
        {
            
        }
    }
    
    public void findRequestsForTargetUser(long userId)
    {
        try
        {
            Map crit = new HashMap();
            //crit.put("Requests.Target Type","U");
            tcResultSet rs = requestOps.getRequestsForUserTarget(userId, new String[]{}, crit);
            logger.debug("print RequestsForTargetUser");
            this.printResultSet(rs);
        }
        catch(Exception e)
        {
            logger.error("API Error",e);
        }
    }
    
    public void cancelRequest(long reqId)
    {
        
        try
        {
            requestOps.cancelRequest(reqId);
        }
        catch(Exception e)
        {
            logger.error("Exception",e);
        }
    }
    

}
